<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('上级地域')" prop="parentId">
            <el-input v-if="isShow" v-model="form.parentName" disabled />
            <treeselect v-if="!isShow" v-model="form.parentId" :options="areaList" :ansyc="true" :load-options="loadOptions" :show-count="true" :placeholder="$t('请选择')">
              <label slot="option-label" slot-scope="{ node, shouldShowCount, count, labelClassName, countClassName }" :class="labelClassName">
                {{ $t(node.label) }}
                <span v-if="shouldShowCount" :class="countClassName">({{ count }})</span>
              </label>
            </treeselect>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('名称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input-number v-model="form.sort" :placeholder="$t('排序')" :controls="false" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注信息')" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="127" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="system:area" />
    </div>
  </div>
</template>

<script>
  import { saveSysArea, getSysArea, treeSelect } from '@/api/system/data/sysArea'
    import Treeselect from '@riophae/vue-treeselect'
    export default {
      components: {
        Treeselect
      },
        data() {
            return {
                // 表单参数
                form: {},
                // 区域树列表
                areaList: [],
                // 遮罩层
                loading: false,
                // 展示区域下拉 还是输入框
                isShow: false,
                // 表单校验
                rules: {
                parentId: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], name: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], sort: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ] }
            }
        },
        created() {
          this.getSelectTree()
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = treeSelect()
                if (data != null && data.id !== undefined) {
                    this.isShow = true
                    getSysArea(data.id).then(response => {
                        this.form = response.data
                        getSysArea(this.form.parentId).then(response => {
                          if (response.data == null) {
                            this.$set(this.form, 'parentName', '主类目')
                          } else {
                            this.$set(this.form, 'parentName', response.data.name)
                          }
                        })
                      this.loading = false
                    })
                } else {
                    this.reset()
                    if (data != null && data.parentId != null) {
                      this.isShow = true
                      this.$set(this.form, 'parentId', data.parentId)
                      this.$set(this.form, 'parentName', data.name)
                    } else {
                      this.isShow = false
                    }
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                parentId: undefined,
                name: undefined,
                sort: undefined,
                createBy: undefined,
                createDate: undefined,
                updateBy: undefined,
                updateDate: undefined,
                remarks: undefined,
                delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveSysArea(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          getSelectTree() {
            treeSelect(null).then(response => {
              this.areaList = []
              const item = {
                id: 0,
                label: this.$t('主类目'),
                children: []
              }
              item.children = response.data
              this.areaList.push(item)
            })
          },
          // eslint-disable-next-line no-unused-vars
          loadOptions({ action, parentNode, callback }) {
            treeSelect({ 'parentId': parentNode.id }).then(response => {
              parentNode.children = response.data
              callback()
            })
          }
        }
    }
</script>
